package com.yunc.modules.shop.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunc.common.schedule.AbstractScheduleJob;
import com.yunc.common.utils.BaseConstant;
import com.yunc.modules.shop.cache.ItemStoreCache;
import com.yunc.modules.shop.entity.Item;
import com.yunc.modules.shop.entity.ItemSpecs;
import com.yunc.modules.shop.service.IItemService;
import com.yunc.modules.shop.service.IItemSpecsService;
import org.apache.commons.lang3.StringUtils;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 商品
 *
 * @author congcong tao
 * @date 2019/1/15 15:51
 */
public class ScheduleItemStore extends AbstractScheduleJob {

    @Autowired
    private IItemService itemService;
    @Autowired
    private IItemSpecsService itemSpecsService;
    @Autowired
    private ItemStoreCache itemStoreCache;

    @Override
    public void before(JobExecutionContext context) {

    }

    @Override
    public void doWork(JobExecutionContext context) {
        // 商品
        List<Item> itemList = itemService.list(new QueryWrapper<Item>().eq("status", BaseConstant.ITEM_STATUS_GROUNDING));
        // 有规格参数商品
        List<ItemSpecs> itemSpecsList = itemSpecsService.list(null);
        if (itemList != null && itemList.size() > 0) {
            for (Item item : itemList) {
                if (!StringUtils.equals(item.getIsParam(), "1")) {
                    itemStoreCache.doInit(item);
                }
            }
        }
        if (itemSpecsList != null && itemSpecsList.size() > 0) {
            for (ItemSpecs itemSpecs : itemSpecsList) {
                itemStoreCache.doInit(itemSpecs);
            }
        }
    }

    @Override
    public void after(JobExecutionContext context) {

    }
}
